home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / news / nntp / nntplink3.1.0 / datafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-25  |  6.1 KB  |  225 lines

  1. #include <fcntl.h>
  2. #include "conf.h"
  3. #ifdef FAKESYSLOG
  4. #include "fsyslog.h"
  5. #else
  6. #include <syslog.h>
  7. #endif
  8. #include <sys/time.h>
  9. #include "readline.h"
  10. #include "nntplink.h"
  11.  
  12. extern char *E_open;
  13. extern char *E_fseek;
  14. extern char *E_fopen;
  15.  
  16. extern Boolean Abort_signaled;
  17. extern Boolean Debug;
  18. extern int Prog_pid;
  19. extern int Input_from;
  20. extern long Success_time;
  21. extern long Report_time;
  22.  
  23. extern void log();
  24. extern double atof();
  25.  
  26. /*
  27.  * Format of the datafile for nntplink:
  28.  *
  29.  *  Line  Description
  30.  *  --------------------------------------------------------------------------
  31.  *   1    Pid of current nntplink (or -999 if none)
  32.  *   2    Version of the datafile (3.0 currently)
  33.  *   3    Inode of the current batchfile
  34.  *   4    Offset into the current batchfile
  35.  *   5    Inode of the current logfile
  36.  *   6    Offset into the current logfile
  37.  *   7    Last time we had a successful transmission of an article
  38.  *   8    Last time we reported a failure
  39.  */
  40.  
  41.  
  42. void
  43.   write_link_datafile(pid)
  44. int pid;
  45. {
  46.     static char *fname = "write_link_datafile: ";
  47.     FILE *link_fp;
  48.     FileBuf *link_fbp;
  49.     int link_fd;
  50.     char *line;
  51.  
  52.     dlog(LOG_DEBUG, fname, "%swriting current info to Datafile\n");
  53.  
  54.     if ((link_fd = open(Host.datafile, O_RDONLY)) == NULL) {
  55.       dlog(LOG_WARNING, fname, E_fopen, Host.name, Host.datafile, "r",
  56.        errmsg(errno));
  57.       return;
  58.     }
  59.  
  60.     link_fbp = fb_fdopen(link_fd);
  61.  
  62.     if ((line = fb_readline(link_fbp, NULL)) == NULL) {
  63.       log(LOG_WARNING, fname, "%s%s: error reading pid from datafile: %s\n",
  64.       Host.name, errmsg(errno));
  65.     } 
  66.  
  67.     close(link_fd);
  68.     fb_close(link_fbp);
  69.  
  70.     if (Prog_pid != (int)atof(line)) {
  71.       if (pid != -999) 
  72.     log(LOG_INFO, fname, "%s%s: pid in datafile has changed, exiting\n",
  73.         Host.name);
  74.       Abort_signaled = TRUE;
  75.     } else {
  76.       if ((link_fp = fopen(Host.datafile, "w")) == NULL) {
  77.     dlog(LOG_WARNING, fname, E_fopen, Host.name, Host.datafile, "w",
  78.          errmsg(errno));
  79.     return;
  80.       }
  81.  
  82.       fprintf(link_fp, "%d\n", pid);
  83.       fprintf(link_fp, "%.1f\n", DATAFILE_VERSION);
  84.       fprintf(link_fp, "%lu\n", (unsigned long)Batchfile.inode);
  85.       if (Batchfile.fbp == NULL)
  86.     fprintf(link_fp, "0\n");
  87.       else
  88.     fprintf(link_fp, "%ld\n", Batchfile.offset);
  89. /*    fprintf(link_fp, "%ld\n", (long)fb_tell(Batchfile.fbp)); */
  90.       fprintf(link_fp, "%lu\n", (unsigned long)Logfile.inode);
  91.       if (Logfile.fbp == NULL)
  92.     fprintf(link_fp, "0\n");
  93.       else
  94.     fprintf(link_fp, "%ld\n", (long)fb_tell(Logfile.fbp));
  95.       fprintf(link_fp, "%ld\n", Success_time);
  96.       fprintf(link_fp, "%ld\n", Report_time);
  97.       FCLOSE(link_fp);
  98.     }
  99. }
  100.  
  101.  
  102. void
  103.   update_link_datafile()
  104. {
  105.     static char *fname = "update_link_datafile: ";
  106.     int link_fd;
  107.     FileBuf *link_fbp;
  108.     char *line;
  109.     float version;
  110.     int errflg = 0, old_pid = 0;
  111.     FILE *link_fp;
  112.  
  113.     dlog(LOG_DEBUG, fname, "%supdating current info in Datafile\n");
  114.  
  115.     if ((link_fd = open(Host.datafile, O_RDONLY)) == FAIL) {
  116.     log(LOG_WARNING, fname, E_open, Host.name, Host.datafile, "r",
  117.         errmsg(errno));
  118.     Success_time = Report_time = time(NULL);
  119.     } else {
  120.     link_fbp = fb_fdopen(link_fd);
  121.  
  122.     if ((line = fb_readline(link_fbp, NULL)) == NULL) {
  123.         log(LOG_WARNING, fname,
  124.         "%s%s: error reading pid from datafile: %s\n",
  125.         Host.name, errmsg(errno));
  126.         errflg++;
  127.     } else if (((old_pid = (int)atof(line)) != 0) &&
  128.            (old_pid != Prog_pid) &&
  129.            (kill(old_pid, 0) == 0) &&
  130.            (Input_from != FLG_STDIN))
  131.         fail(fname, "%s%s: nntplink(%d) process already running\n",
  132.          Host.name, old_pid);
  133.  
  134.     if (!errflg)
  135.       if ((line = fb_readline(link_fbp, NULL)) == NULL) {
  136.           log(LOG_WARNING, fname,
  137.           "%s%s: error reading version from datafile: %s\n",
  138.           Host.name, errmsg(errno));
  139.           errflg++;
  140.       } else {
  141.           version = (float)(((int)(atof(line) * 10)) / 10.0);
  142.           if (version!= DATAFILE_VERSION) {
  143.           log(LOG_WARNING, fname,
  144.               "%s%s: datafile version(%.1f) not same as current version(%.1f)\n",
  145.               Host.name, version, DATAFILE_VERSION);
  146.           errflg++;
  147.           }
  148.       }
  149.  
  150.     if (!errflg)
  151.       if ((line = fb_readline(link_fbp, NULL)) == NULL) {
  152.           log(LOG_WARNING, fname,
  153.           "%s%s: error reading batchfile inode from datafile: %s\n",
  154.           Host.name, errmsg(errno));
  155.           errflg++;
  156.       } else
  157.         Batchfile.inode = (ino_t)atol(line);
  158.  
  159.     if (!errflg)
  160.       if (((line = fb_readline(link_fbp, NULL)) == NULL) ||
  161.           ((Batchfile.offset = (off_t)atol(line)) < 0)) {
  162.           log(LOG_WARNING, fname,
  163.           "%s%s: error reading batchfile offset from datafile: %s\n",
  164.           Host.name, errmsg(errno));
  165.           errflg++;
  166.       }
  167.  
  168.     if (!errflg)
  169.       if ((line = fb_readline(link_fbp, NULL)) == NULL) {
  170.           log(LOG_WARNING, fname,
  171.           "%s%s: error reading logfile inode from datafile: %s\n",
  172.           Host.name, errmsg(errno));
  173.           errflg++;
  174.       } else
  175.         Logfile.inode = (ino_t)atol(line);
  176.  
  177.     if (!errflg)
  178.       if (((line = fb_readline(link_fbp, NULL)) == NULL) ||
  179.           ((Logfile.offset = (off_t)atol(line)) < 0)) {
  180.           log(LOG_WARNING, fname,
  181.           "%s%s: error reading logfile offset from datafile: %s\n",
  182.           Host.name, errmsg(errno));
  183.           errflg++;
  184.       }
  185.  
  186.     if (!errflg)
  187.       if (((line = fb_readline(link_fbp, NULL)) == NULL) ||
  188.           ((Success_time = atol(line)) < 0)) {
  189.           log(LOG_WARNING, fname,
  190.           "%s%s: error reading success time from datafile: %s\n",
  191.           Host.name, errmsg(errno));
  192.           errflg++;
  193.       }
  194.  
  195.     if (!errflg)
  196.       if (((line = fb_readline(link_fbp, NULL)) == NULL) ||
  197.           ((Report_time = atol(line)) < 0)) {
  198.           log(LOG_WARNING, fname,
  199.           "%s%s: error reading report time from datafile: %s\n",
  200.           Host.name, errmsg(errno));
  201.           errflg++;
  202.       }
  203.  
  204.     close(link_fd);
  205.     fb_close(link_fbp);
  206.     }
  207.  
  208.     if ((link_fp = fopen(Host.datafile, "w")) == NULL) {
  209.       dlog(LOG_WARNING, fname, E_fopen, Host.name, Host.datafile, "w",
  210.        errmsg(errno));
  211.     } else {
  212.     fprintf(link_fp, "%d\n", Prog_pid);
  213.     fprintf(link_fp, "%.1f\n", DATAFILE_VERSION);
  214.     fprintf(link_fp, "%lu\n", (unsigned long)Batchfile.inode);
  215.     fprintf(link_fp, "%ld\n", (long)Batchfile.offset);
  216.     fprintf(link_fp, "%lu\n", (unsigned long)Logfile.inode);
  217.     fprintf(link_fp, "%ld\n", (long)Logfile.offset);
  218.     fprintf(link_fp, "%ld\n", Success_time);
  219.     fprintf(link_fp, "%ld\n", Report_time);
  220.     FCLOSE(link_fp);
  221.     }
  222.  
  223.     return;
  224. }
  225.